<?php
namespace app\controllers;

use app\models\Project;
use Yii;
use yii\helpers\Url;
use yii\web\Controller;

class BasisController extends Controller
{
    public function beforeAction($action)
    {
        parent::beforeAction($action);

        // 判断项目是不是公开的
        $projectId = Yii::$app->request->get('projectId','');
        if ($this->isPublic($projectId))return true;

        if(Yii::$app->user->isGuest){
            $redirect = Yii::$app->request->getAbsoluteUrl();
            return $this->redirect(Url::toRoute(['/auth/login','redirect' => $redirect]));
        }
        return true;
    }

    protected function isPublic($projectId)
    {
        if (!$projectId)return false;
        $projectInfo = Project::find()->where(['id' => $projectId])->asArray()->one();

        if (empty($projectInfo))return false;

        if (!isset($projectInfo['is_public']))return false;

        return $projectInfo['is_public'];

    }
    public function ajaxReturn($array)
    {
        echo json_encode($array,JSON_UNESCAPED_UNICODE);
        Yii::$app->end();
    }
}